<!--
 * @Descripttion:
 * @Author: xp
 * @Date: 2022-09-02 16:59:44
-->
<template>
  <el-breadcrumb class="app-breadcrumb" separator="/">
    <transition-group name="breadcrumb">
      <el-breadcrumb-item v-for="(item, index) in levelList" :key="item.path">
        <span
          v-if="item.redirect === 'noRedirect' || index == levelList.length - 1"
          style="color: rgba(0, 0, 0, 0.45)"
          class="no-redirect"
          >{{ getMetaTitle(item) }}</span
        >
        <a v-else @click.prevent="handleLink(item)">{{ item.meta.title }}</a>
      </el-breadcrumb-item>
    </transition-group>
  </el-breadcrumb>
</template>

<script>
import pathToRegexp from "path-to-regexp";
import { mapGetters } from "vuex";

export default {
  data() {
    return {
      levelList: [],
    };
  },
  watch: {
    $route(route) {
      // if you go to the redirect page, do not update the breadcrumbs
      if (route.path.startsWith("/redirect/")) {
        return;
      }
      this.getBreadcrumb();
    },
  },
  created() {
    this.getBreadcrumb();
  },

  computed: {
    ...mapGetters(["sidebar", "roleRouter"]),
    getRouters() {
      return this.roleRouter;
    },
  },
  methods: {
    getMetaTitle(item) {
      return item.meta.breadTitle ? item.meta.breadTitle : item.name;
    },
    // 获取匹配到得多层级路由
    getSelectedRoute(list, matchedArr, index) {
      list.map(item => {
        if (item.children && item.children.length > 0) {
          let ParentSelectedItem = null
          let selectedItem = item.children.find(item1 => item1.path == matchedArr[matchedArr.length - 1].meta.activeMenu)
          // 当前路由有非二级页面的列表 故查询出来  加入面包屑
          if (matchedArr[matchedArr.length - 1].meta.showTitle) {
            ParentSelectedItem = item.children.find(item1 => item1.path == matchedArr[matchedArr.length - 1].meta.showTitle)
          }
          if (selectedItem) {
            if (ParentSelectedItem) {
              matchedArr.splice(index, 0, ParentSelectedItem)
            }
            matchedArr.splice(index, 0, selectedItem)
          } else {
            this.getSelectedRoute(item.children, matchedArr, index+1)
          }
        }
      })
    },
    getBreadcrumb() {
      // only show routes with meta.title
      let matched = this.$route.matched.filter((item) => item.meta && item.meta.title);
      if (matched[matched.length - 1].meta.activeMenu) {
        this.getSelectedRoute(this.roleRouter, matched, 1)
        this.levelList = matched;
      } else {
        matched.map((item, index) => {
          console.log(item, "item");
          if (index == 1) {
            console.log(this.levelList);
          }
        });
        this.levelList = matched;
      }
      if (this.levelList.length > 3) {
        this.levelList = matched;
      }
    },
    isHome(route) {
      const name = route && route.name;
      if (!name) {
        return false;
      }
      return name.trim().toLocaleLowerCase() === "home".toLocaleLowerCase();
    },
    pathCompile(path) {
      // To solve this problem https://github.com/PanJiaChen/vue-element-admin/issues/561
      const { params } = this.$route;
      var toPath = pathToRegexp.compile(path);
      return toPath(params);
    },
    handleLink(item) {
      const { redirect, path, parent } = item;
      if (parent && parent.path === '/marketingManage') {
        this.$router.go(-1);
        return
      }
      if (redirect) {
        this.$router.push(redirect);
        return;
      }
      this.$router.push(this.pathCompile(path));
    },
  },
};
</script>

<style lang="scss" scoped>
.app-breadcrumb.el-breadcrumb {
  display: inline-block;
  font-size: 14px;
  line-height: 24px;
  margin: 16px 0 16px 16px;
  .no-redirect {
    color: #fff;
    cursor: text;
  }
}
</style>
